﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using System.Collections.ObjectModel;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Activities.Chooser
{
    public partial class UserForm : Form
    {
        private IList<TNTreeNode> selectedNodes;
        /// <summary>
        /// 被选中的节点
        /// </summary>
        public IList<TNTreeNode> SelectedNodes
        {
            get { return selectedNodes; }
            set { selectedNodes = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 被选中的用户名称
        /// </summary>
        public string UserNames
        {
            get
            {
                string ret = string.Empty;
                selectedNodes = new List<TNTreeNode>();
                GetSelectedNodes((TNTreeNode)this.treeView1.Nodes[0]);
                for (int i = 0; i < selectedNodes.Count; i++)
                {
                    ret += selectedNodes[i].ID + ",";
                }

                if (ret.EndsWith(","))
                {
                    ret = ret.Remove(ret.Length - 1);
                }

                this.textBox1.Text = ret;
                return ret;
            }
            set { this.textBox1.Text = value; }
        }


        private void GetSelectedNodes(TNTreeNode currentNode)
        {
            if (currentNode.NodeType == TNTreeNodeType.User)
            {
                if (currentNode.Checked == true)
                {
                    selectedNodes.Add(currentNode);
                }
            }

            if (currentNode.Nodes.Count > 0)
            {
                foreach (TNTreeNode tempNode in currentNode.Nodes)
                {
                    if (currentNode.NodeType == TNTreeNodeType.Organization)
                    {
                        if (currentNode.Checked == true)
                        {
                            ReadOnlyCollection<IUser> userColl= ChinaCustomsPermissionWrapper.GetUsersInOrganizations(currentNode.ID);
                            for (int i = 0; i < userColl.Count; i++)
                            {
                                TNTreeNode node = new TNTreeNode();
                                node.ID = userColl[i].LogOnName;
                                node.Name = userColl[i].DisplayName;
                                node.NodeType = TNTreeNodeType.User;
                                selectedNodes.Add(node);
                            }
                        }
                        else
                        {
                            GetSelectedNodes(tempNode);
                        }
                    }
                }
            }
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            this.BuildeTree();
        }

        private void BuildeTree()
        {
            this.treeView1.ShowLines = true;
            this.treeView1.ShowNodeToolTips = true;
            this.treeView1.ShowPlusMinus = true;
            this.treeView1.ShowRootLines = true;
            this.treeView1.Nodes.Clear();
            this.treeView1.CheckBoxes = true;

            IOrganization rootOrg = ChinaCustomsPermissionWrapper.GetRootOrganization();

            Dictionary<string, IOrganization> organizationCollectionAll = null;
            Dictionary<string, string> needDisplayOrg = null;
            ChinaCustomsPermissionWrapper.GetObjectsSearched(rootOrg,out needDisplayOrg, out organizationCollectionAll);

            if (rootOrg != null)
            {
                TNTreeNode rootNode = new TNTreeNode();
                rootNode.ID = rootOrg.ID;
                rootNode.Text = rootOrg.Name;
                rootNode.NodeType = TNTreeNodeType.Organization;
                this.treeView1.Nodes.Add(rootNode);
                this.BuildeTreeNode(rootNode, needDisplayOrg, organizationCollectionAll);
            }
        }

        private void BuildeTreeNode(TNTreeNode parentNode, Dictionary<string, string> needDisplayOrg, Dictionary<string, IOrganization> organizationCollectionAll)
        {
            if (needDisplayOrg == null || organizationCollectionAll == null)
            {
                return;
            }
            else
            {
                string parentID = parentNode.ID;

                foreach (KeyValuePair<string, string> kvpOrg in needDisplayOrg)
                {
                    if (kvpOrg.Value == parentID)
                    {
                        TNTreeNode node = new TNTreeNode();
                        node.ID = kvpOrg.Key;
                        node.Text = organizationCollectionAll[kvpOrg.Key].DisplayName;


                        this.BuildeTreeNode(node, needDisplayOrg, organizationCollectionAll);
                        parentNode.Nodes.Add(node);
                    }
                }
            }
        }

        private void BuildeNextLevelNode(TNTreeNode parentNode)
        {
            string[] parentIDArray = new string[1];
            parentIDArray[0] = parentNode.ID;

            ReadOnlyCollection<IUser> users = ChinaCustomsPermissionWrapper.GetUsersInOrganizations(parentIDArray, false);
            for (int i = 0; i < users.Count; i++)
            {
                TNTreeNode node = new TNTreeNode();
                node.ID = users[i].LogOnName;
                node.Text = users[i].DisplayName;
                node.NodeType = TNTreeNodeType.User;
                parentNode.Nodes.Add(node);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TNTreeNode node = e.Node as TNTreeNode;
            if (node == null || node.NodeType == TNTreeNodeType.User)
            {
                return;
            }

            if (node.IsAddedChildren == false)
            {
                string nodeID = node.ID;
                this.BuildeNextLevelNode(node);
                node.IsAddedChildren = true;
            }
        }
    }
}
